package com.two.screen.water.balance.ui.activity


import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.gyf.barlibrary.BarHide
import com.gyf.barlibrary.ImmersionBar
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.bean.BaseData
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.ui.activity.base.BaseActivity
import com.two.screen.water.balance.utils.ARouterUtils
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.getColor
import com.two.screen.water.balance.widget.DividerItemDecoration
import com.two.screen.water.balance.widget.dialog.FilterOrderDialog01
import com.two.screen.water.balance.widget.dialog.LoadingDialog
import kotlinx.android.synthetic.main.activity_cashier_order.*
import java.util.concurrent.TimeUnit

@Route(path = ARouterUtils.CashierOrderActivity)
class CashierOrderActivity : BaseActivity() {
    val TAG = CashierOrderActivity::class.java.simpleName.plus("=>")
    var loadingDialog: LoadingDialog? = null

    private lateinit var mSearchChooseAdapter: SearchChooseAdapter
    private lateinit var mGoodsAdapter: GoodsAdapter

    /*对话框*/
    private var mFilterOrderDialog01: FilterOrderDialog01? = null

    override fun getContentLayout(): Any = R.layout.activity_cashier_order
    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this)
            .hideBar(BarHide.FLAG_HIDE_BAR)
            .init()
    }

    override fun initData() {

        initSearchRecyclerView()
        initGoodsRecyclerView()
    }

    private fun initGoodsRecyclerView() {
        (mProductRecyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false //去闪烁
        mProductRecyclerView?.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL_LIST, 1, ContextCompat.getColor(App.context!!, R.color.c6)))
        mGoodsAdapter = GoodsAdapter()
        mProductRecyclerView.layoutManager = LinearLayoutManager(this)
        mProductRecyclerView.adapter = mGoodsAdapter
        mGoodsAdapter.bindToRecyclerView(mProductRecyclerView)
        mGoodsAdapter.setEmptyView(R.layout.layout_status_layout_manager_empty3)
        mGoodsAdapter.setNewData(mutableListOf(BaseData(), BaseData(), BaseData(), BaseData(), BaseData(), BaseData(), BaseData(), BaseData(), BaseData()))
    }

    /*搜索列表*/
    private fun initSearchRecyclerView() {
        (mSearchRecyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false //去闪烁
        mSearchRecyclerView?.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL_LIST, 1, ContextCompat.getColor(App.context!!, R.color.c6)))
        mSearchChooseAdapter = SearchChooseAdapter()
        mSearchRecyclerView.layoutManager = LinearLayoutManager(this)
        mSearchRecyclerView.adapter = mSearchChooseAdapter
        mSearchChooseAdapter.bindToRecyclerView(mSearchRecyclerView)
        mSearchChooseAdapter.setEmptyView(R.layout.layout_status_layout_manager_empty3)

        //测试数据
        mSearchChooseAdapter.setNewData(mutableListOf(BaseData(), BaseData(), BaseData(), BaseData(), BaseData(), BaseData(), BaseData(), BaseData(), BaseData(), BaseData(), BaseData()))
        mSearchChooseAdapter.data.first().isSelected = true
        mSearchChooseAdapter.notifyItemChanged(0)
    }


    @SuppressLint("CheckResult")
    override fun initListener() {
        mCloseIv.setOnClickListener { finish() }
        RxView.clicks(mFilterIv)
            .throttleFirst(1, TimeUnit.SECONDS)
            .subscribe {
                if (mFilterOrderDialog01 == null) mFilterOrderDialog01 = FilterOrderDialog01(this)
                mFilterOrderDialog01?.show()
            }
    }

    /*搜索列表 适配器*/
    inner class SearchChooseAdapter : BaseQuickAdapter<BaseData, BaseViewHolder>(R.layout.item_order_search, null) {
        @SuppressLint("CheckResult", "NotifyDataSetChanged")
        override fun convert(helper: BaseViewHolder, item: BaseData) {
            var orderSnAtv = helper.getView<TextView>(R.id.item_order_sn_atv).apply {
                setTextColor(if (item.isSelected) CommUtils.getColor(R.color.GB79C5F) else CommUtils.getColor(R.color.G567485))
            }
            var orderPriceAtv = helper.getView<TextView>(R.id.item_order_price_atv).apply {
                setTextColor(if (item.isSelected) CommUtils.getColor(R.color.GB79C5F) else CommUtils.getColor(R.color.G567485))
            }

            helper.getView<View>(R.id.item_root_view).apply {
                setBackgroundColor(if (item.isSelected) CommUtils.getColor(R.color.GF4EFE5) else CommUtils.getColor(R.color.f6))
                setOnClickListener {
                    data.forEach { it.isSelected = false }
                    item.isSelected = true
                    notifyDataSetChanged()
                    //TODO:同时刷新右边页面。
                }
            }

            //.setText(R.id.item_tv1, String.format("%02d", helper.bindingAdapterPosition + 1).plus("."))
        }
    }

    /*商品列表 适配器*/
    inner class GoodsAdapter : BaseQuickAdapter<BaseData, BaseViewHolder>(R.layout.item_order_goods, null) {
        @SuppressLint("CheckResult")
        override fun convert(helper: BaseViewHolder, item: BaseData) {
            helper
                .setText(R.id.item_tv1, String.format("%02d", helper.bindingAdapterPosition + 1))
        }
    }
}